//Constructor to the general Entity object, which
//Is the supertype for all objects in the game space.
//Note that position is a JSON containing both the entity's
//X and Y positions, so that x position is accessed via
//pos.x, and y via pos.y
Entity = function(angle, orbit, direction, width, height, sprite){
	this.orbit = orbit;
	this.direction = direction;
	this.angle = angle;
	this.distance = Orbit_getDistance(orbit);
	this.pos = Orbit_getPos(orbit, angle);
	this.width = width;
	this.height = height;
	this.angVel = Orbit_getAngVelClosed(orbit, direction);
	this.sprite = new Image();
	this.sprite.src = sprite;
	
	//Checks collision between the given entity and another entity
	this.checkCollision = function(other){
		//doesn't work if the other sprite is much larger than this one, but performance...
		if (other.pos.x > (this.pos.x + this.width*2) || other.pos.x < (this.pos.x - this.width*2) ||
			other.pos.y > (this.pos.y + this.height*2) || other.pos.y < (this.pos.y - this.height*2)) return false;
		
		var sthis = Math.sin(this.angle);
		var cthis = Math.cos(this.angle);
		
		var sother = Math.sin(other.angle);
		var cother = Math.cos(other.angle);
		
		var ns = Math.sin(-this.angle);
		var nc = Math.cos(-this.angle);
		
		//[0] = left/top corner
		var thiscorners  = [{x: this.pos.x-this.width/2, y: this.pos.y-this.height/2}, {x: this.pos.x+this.width/2, y: this.pos.y+this.height/2}];
		var othercorners = [{x: other.pos.x-other.width/2, y: other.pos.y-other.height/2}, {x: other.pos.x+other.width/2, y: other.pos.y+other.height/2}];
	
		var nthiscorners  = [{x: this.pos.x-this.width/2, y: this.pos.y-this.height/2}, {x: this.pos.x+this.width/2, y: this.pos.y+this.height/2}];
		var nothercorners = [{x: other.pos.x-other.width/2, y: other.pos.y-other.height/2}, {x: other.pos.x+other.width/2, y: other.pos.y+other.height/2}];
	
	
		for (var i = 0; i < 2; i++) {
			thiscorners[i].x -= this.pos.x;
			thiscorners[i].y -= this.pos.y;
			
			nthiscorners[i].x = thiscorners[i].x*cthis-thiscorners[i].y*sthis;
			nthiscorners[i].y = thiscorners[i].x*sthis+thiscorners[i].y*cthis;
			
			thiscorners[i].x = nthiscorners[i].x + this.pos.x;
			thiscorners[i].y = nthiscorners[i].y + this.pos.y;
			
			
			othercorners[i].x -= other.pos.x;
			othercorners[i].y -= other.pos.y;
			
			nothercorners[i].x = othercorners[i].x*cother-othercorners[i].y*sother;
			nothercorners[i].y = othercorners[i].x*sother+othercorners[i].y*cother;
			
			othercorners[i].x = nothercorners[i].x + other.pos.x;
			othercorners[i].y = nothercorners[i].y + other.pos.y;
			
						
			nthiscorners[i].x = thiscorners[i].x*nc-thiscorners[i].y*ns;
			nthiscorners[i].y = thiscorners[i].x*ns+thiscorners[i].y*nc;
			
			nothercorners[i].x = othercorners[i].x*nc-othercorners[i].y*ns;
			nothercorners[i].y = othercorners[i].x*ns+othercorners[i].y*nc;
		}

	
		return  ((nothercorners[1].x > nthiscorners[0].x) && (nothercorners[0].x < nthiscorners[1].x)) &&
				((nothercorners[1].y > nthiscorners[0].y) && (nothercorners[0].y < nthiscorners[1].y));
	};
	
	//Updates the current position of the given entity
	this.updatePosition = function(){
		this.angle = (this.angle + this.angVel)%(Math.PI*2);
		if(this.angle < 0){this.angle = Math.PI*2 + this.angle;}
		this.pos = Orbit_getPos(this.orbit, this.angle);
	};
	
	//Draws the given entity onto the screen
	this.drawEntity = function(angleOffset){
		angleOffset = (typeof angleOffset === "undefined") ? 0 : angleOffset;
		
		if((this.pos.x > player.pos.x - canvas.width &&
			this.pos.x < player.pos.x + canvas.width) &&
			(this.pos.y > player.pos.y - canvas.height &&
			this.pos.y < player.pos.y + canvas.height))
		{
			ctx.save();
			ctx.translate(this.pos.x, this.pos.y);
			ctx.rotate(angleOffset);
			ctx.translate(-this.pos.x, -this.pos.y);
			ctx.rotate(this.angle);
			var dist = Orbit_getPlanetDistance(this.pos.x, this.pos.y);
			ctx.drawImage(this.sprite, Orbit_getPosFree(dist, 0).x-(this.width/2), Orbit_getPosFree(dist, 0).y-(this.height/2));
			ctx.restore();
		}
	};
};


